from masoniteorm.schema import Schema
from masoniteorm.relationships import belongs_to, has_many

from application.config import appSettings
from application.config.database import DB
from ..base import BaseModel


class AssetCategory(BaseModel):
    __table__ = appSettings.db_prefix + 'asset_categories'

    @belongs_to('brand_id', 'brand_id')
    def brand(self):
        from .Company import Company
        return Company

    @has_many('id', 'parent_id')
    def children(self):
        return AssetCategory

    @staticmethod
    def create_table():
        table_name = __class__.__table__
        # 动态创建表
        mSchema = Schema(connection=__class__.__connection__, connection_details=DB.get_connection_details())
        # 如果没有表则创建
        if not mSchema.has_table(table_name):
            with mSchema.create(table_name) as table:
                table.big_increments('id').primary()
                table.integer('brand_id', 11, True).index().comment('品牌ID')
                table.string('brand_code', 32, True).index().comment('品牌旧 Code')
                table.string('name', 32, True).index().comment('素材分类名称')
                table.big_integer('parent_id').default(0).index().comment('父级ID')
                table.integer('level', 11).default(1).comment('level 1级 2级 3级 最多')
                table.integer('status', 11, True).default(1).index().comment('素材分类状态 1 启用 0 禁用')
                # created_at/updated_at
                table.timestamp('created_at', True, True)
                table.timestamp('updated_at', True, True)


class Asset(BaseModel):
    __table__ = appSettings.db_prefix + 'assets'

    @belongs_to('brand_id', 'brand_id')
    def brand(self):
        from .Company import Company
        return Company

    @staticmethod
    def create_table():
        table_name = __class__.__table__
        # 动态创建表
        mSchema = Schema(connection=__class__.__connection__, connection_details=DB.get_connection_details())
        # 如果没有表则创建
        if not mSchema.has_table(table_name):
            with mSchema.create(table_name) as table:
                table.big_increments('id').primary()
                table.integer('category_id', 11).default(0).index().comment('分类ID')
                table.integer('brand_id', 11, True).index().comment('品牌ID')
                table.string('brand_code', 32, True).index().comment('品牌旧 Code')
                table.integer('type', 11).default(1).index().comment('类型 1 图片 2 文件')
                table.integer('position', 11).default(1).index().comment('存储位置 0 local 1 Qiniu 2 OSS')
                table.integer('is_public', 11).default(1).index().comment('公开 1 是 0 否')
                table.string('name', 255).nullable().comment('文件名称')
                table.string('path', 255).nullable().comment('文件路径')
                table.string('url', 255).nullable().comment('文件url前缀')
                table.integer('admin_id', 11).default(0).index().comment('上传管理用户ID')
                # created_at/updated_at
                table.timestamp('created_at', True, True)
                table.timestamp('updated_at', True, True)


__all__ = [
    'Asset',
    'AssetCategory',
]
